﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Story2dEnterGame : MonoBehaviour
{

    public List<Story2dLogic> storys;

    public int currentStoryIndex = 0;

    public StoryEndCallback endcallback; 

    public void Play()
    {
        foreach (var story in storys)
        {
            story.SetStoryEndCallback(OnStoryEnd);
        }
        currentStoryIndex = 0;
        Play0(currentStoryIndex);
    }

    void Play0(int index)
    {
        if (storys.Count > index)
        {
            storys[index].StoryStart();
            currentStoryIndex = index;
        }
    }

    public void Skip()
    {
        ManagerController.Instance.EnterGameStoryDone(gameObject);
        if (endcallback != null)
        {
            endcallback();
        }
    }

    public void OnStoryEnd()
    {
        Debug.Log("Story End");
        GameObject.Destroy(storys[currentStoryIndex].gameObject);
        Resources.UnloadUnusedAssets();
        currentStoryIndex++;
        if (currentStoryIndex < storys.Count)
        {
            Play0(currentStoryIndex);
        }
        else
        {
            ManagerController.Instance.EnterGameStoryDone(gameObject);
            if (endcallback != null)
            {
                endcallback();
            }
        }
    }



}